//package com.vast.admin.controller;
//
//
//import cn.hutool.core.date.DateTime;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.vast.common.utils.StringUtils;
//import com.google.code.kaptcha.Constants;
//import com.google.code.kaptcha.Producer;
//import com.vast.admin.annotation.CurrentManager;
//import com.vast.admin.annotation.PassToken;
//import com.vast.admin.annotation.UseToken;
//import com.vast.admin.entity.Company;
//import com.vast.common.core.domain.entity.SysUser;
//import com.vast.admin.entity.ManagerRole;
//import com.vast.admin.entity.SysLog;
//import com.vast.admin.service.ManagerRoleService;
//import com.vast.admin.service.ISysUserService;
//import com.vast.admin.service.SysLogService;
//import com.vast.admin.service.impl.CompanyService;
//import com.vast.common.constant.MessageCode;
//import com.vast.common.core.domain.Result;
//import com.vast.system.shiro.ShiroUtils;
//import com.vast.system.utils.Constant;
//import com.vast.system.utils.IPUtils;
//import com.vast.system.utils.JWTUtil;
//import io.swagger.annotations.*;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.imageio.ImageIO;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.awt.image.BufferedImage;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//
///**
// * <p>
// * 登录注册 前端控制器
// * </p>
// *
// * @author zhaohao
// * @since 2018-10-24
// */
//@RestController
//@Api(value = "登录注册相关的api", tags = { "登录注册模块" })
//@RequestMapping("/manager")
//public class LoginController {
//
//
//	@Autowired
//	private SysLogService sysLogService;
//
//	@Autowired
//	private ISysUserService userService;
//
//	@Autowired
//	private Producer producer;
//
//	@Autowired
//	private CompanyService companyService;
//
//	@Autowired
//	private ManagerRoleService managerRoleService;
//
//	//session过期,未授权, 重定向到登陆页
//	@RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET)
//	public Result index() {
//		return new Result<>(MessageCode.SESSION_EXPIRE, "未授权或授权过期,请重新登录");
//	}
//
//	/**
//	 * 获取验证码
//	 * @param response
//	 * @throws IOException
//	 */
//	@RequestMapping("captcha.jpg")
//	public void captcha(HttpServletResponse response)throws IOException {
//		response.setHeader("Cache-Control", "no-store, no-cache");
//		response.setContentType("image/jpeg");
//
//		//生成文字验证码
//		String text = producer.createText();
//		//生成图片验证码
//		BufferedImage image = producer.createImage(text);
//		//保存到shiro session
//		ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
//
//		ServletOutputStream out = response.getOutputStream();
//		ImageIO.write(image, "jpg", out);
//	}
//
//
//	// 登录
//	@ApiOperation(value = "用户登录接口", notes = "根据用户名,密码登录")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query", defaultValue = "13419519796"),
//			@ApiImplicitParam(name = "pwd", value = "用户密码", required = true, dataType = "String", paramType = "query", defaultValue = "123456"),
//			@ApiImplicitParam(name = "captcha", value = "验证码", required = true, dataType = "String", paramType = "query") })
//	@PostMapping("/login")
//	@PassToken
//	public Result<SysUser> login(String username, String pwd, String captcha) {
//		String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
//		if(!captcha.equalsIgnoreCase(kaptcha)){
//			return new Result<>(MessageCode.PARAM_ERROR, "验证码不正确");
//		}
//
//		if(StringUtils.isEmpty(username)){
//			return new Result<>(MessageCode.PARAM_ERROR, "账号不能为空");
//		}
//
//		if(StringUtils.isEmpty(pwd)){
//			return new Result<>(MessageCode.PARAM_ERROR, "密码不能为空");
//		}
//
//		if(userService.checkAccount(username) == 0){
//			return new Result<>(MessageCode.ERROR, "账号不存在");
//		}
//
//		Subject subject = ShiroUtils.getSubject();
//		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
//		try {
//			subject.login(token);
//		} catch (Exception e) {
//			return new Result<>(MessageCode.ERROR, "账号或密码错误");
//		}
//		SysUser manager = (SysUser) subject.getPrincipal();
//		manager.setToken(JWTUtil.sign(manager.getUserId() + "", manager.getPassword()));
//		List<ManagerRole> managerRoles = managerRoleService.list(new QueryWrapper<ManagerRole>().eq(ManagerRole.COL_MANAGER_ID,manager.getUserId()));
//		List<Long> roleIds = new ArrayList<>();
//		for (ManagerRole managerRole : managerRoles) {
//			roleIds.add(managerRole.getRoleId());
//		}
//		Long[] roleIdArr = new Long[roleIds.size()];
//		roleIds.toArray(roleIdArr);
//		manager.setRoleIdList(roleIdArr);
//		if(!Arrays.asList(manager.getRoleIdList()).contains(Constant.SUPER_ADMIN)){//超级管理员
//			if(manager.getCompanyId() != null && manager.getCompanyId() > 0){
//				Company company = companyService.getById(manager.getCompanyId());
//				if(company != null){
//					manager.setCompanyName(company.getName());
//				}
//			}
//
//		}else{
//			manager.setCompanyName("系统管理");
//		}
//
////		SysUser manager = userService.login(username, password);
////		if (manager == null) {
////			return new Result<>(MessageCode.ERROR, "账号或密码错误");
////		} else if (manager.getStatus() == 0) {
////			//保存登录日志
////			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
////			SysLog sysLog = new SysLog();
////			sysLog.setUsername(manager.getUsername());
////			sysLog.setMethod(request.getMethod());
////			sysLog.setOperation("登录");
////			sysLog.setCreateTime(DateTime.now());
////			sysLog.setValid(1);
////			//获取用户ip地址
////			sysLog.setIp(IPUtils.getIpAddr(request));
////			sysLog.setUrl(request.getRequestURL().toString());
////			sysLog.setValid(1);
////			sysLog.setType(1); //登录日志
////			sysLog.setStatus(2);
////			sysLogService.save(sysLog);
////			return new Result<>(MessageCode.ERROR, "用户已被禁用");
////		} else {
////			//保存登录日志
//			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//			String ip = request.getHeader("real-ip");
//			SysLog sysLog = new SysLog();
//			sysLog.setUsername(manager.getName());
//			sysLog.setMethod( request.getMethod());
//			sysLog.setOperation("登录");
//			sysLog.setCreateTime(DateTime.now());
//			sysLog.setValid(1);
//			//获取用户ip地址
//			sysLog.setIp(ip);
//			sysLog.setUrl(request.getRequestURL().toString());
//			sysLog.setValid(1);
//			sysLog.setType(1); //登录日志
//			sysLog.setStatus(1);
//			sysLogService.save(sysLog);
////			manager.setToken(JWTUtil.sign(manager.getUserId() + "", password));
////			return new Result<SysUser>(MessageCode.SUCCESS, "登录成功", manager);
////		}
//		return new Result<SysUser>(MessageCode.SUCCESS, "登录成功",manager);
//	}
//
//
//
////	@ApiOperation(value = "用户注册接口", notes = "根据type注册不同用户类型")
//////	@ApiImplicitParams({
//////			// 注意：paramType需要指定为body
//////			@ApiImplicitParam(paramType = "body", name = "user", value = "用户数据", dataType = "User", required = true)
//////			})
////	@PassToken
////	@PostMapping("/regist")
////	public Result regist(@RequestBody @ApiParam(name="User",value="传入json格式",required=true) User user) {
////		return new Result(MessageCode.SUCCESS, "注册成功", userService.regist(user));
////	}
////
//	// 退出
//	@ApiOperation(value = "用户注销接口", notes = "根据token注销")
//	@ApiImplicitParams({
//			@ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true) })
//	@PostMapping("/logout")
//	@UseToken
//	public Result<Boolean> logout(@ApiParam(hidden = true) @CurrentManager SysUser manager) {
//		manager.setToken("");
//		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//		SysLog sysLog = new SysLog();
//		sysLog.setUsername(manager.getName());
//		sysLog.setMethod( request.getMethod());
//		sysLog.setOperation("退出");
//		sysLog.setCreateTime(DateTime.now());
//		sysLog.setValid(1);
//		//获取用户ip地址
//		sysLog.setIp(IPUtils.getIpAddr(request));
//		sysLog.setUrl(request.getRequestURL().toString());
//		sysLog.setValid(1);
//		sysLog.setType(1); //登录日志
//		sysLog.setStatus(1);
//		sysLogService.save(sysLog);
//		ShiroUtils.logout();
//		return new Result<Boolean>(MessageCode.SUCCESS, "退出成功",true);
//	}
////
//	// 检测手机号是否注册
//	@ApiOperation(value = "检测手机号是否注册", notes = "检测手机号是否注册")
//	@ApiImplicitParams({
//			@ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", dataType = "String", required = true) })
//	@PostMapping("/checkAccount")
//	@PassToken
//	public Result checkUser(String phone) {
//		SysUser manager = userService.getOne(new QueryWrapper<SysUser>().eq(SysUser.COL_ACCOUNT, phone));
//		if (manager != null) {
//			return new Result(MessageCode.EXIST_USER, "用户已存在");
//		}
//		return new Result(MessageCode.UNDEFINED_USER, "用户不存在");
//	}
//
//}
